﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace UIT_CV_Management
{
    public class LyLichSoLuoc
    {
        Database myDatabase = Database.shareDatabase;
        public String maLyLichSoLuoc;
        public String maNgheNghiep;
        public String maThongTinPhu;

        public String hoTen;
        public DateTime ngaySinh;
        public String phai;
        public String biDanh;
        public String noiSinh;

        //object
        public NgheNghiep ngheNghiep
        {
            get
            {
                return new NgheNghiep(maNgheNghiep);
            }
            set
            {
                ngheNghiep = value;
            }
        }
        public ThongTinPhu thongTinPhu
        {
            get
            {
                if (maThongTinPhu != "")
                    return new ThongTinPhu(maThongTinPhu);
                return null;
            }

            set
            {
                thongTinPhu = value;
            }
        }
        public LyLichSoLuoc(String _hoTen, DateTime _ngaySinh, String _noiSinh, String _phai, String _biDanh, ThongTinPhu _thongTinPhu, NgheNghiep _ngheNghiep)
        {
            maLyLichSoLuoc = Database.getID("LyLichSoLuoc", "MaLyLichSoLuoc");

            hoTen = _hoTen;
            ngaySinh = _ngaySinh;
            phai = _phai;
            biDanh = _biDanh;
            noiSinh = _noiSinh;
            maNgheNghiep = _ngheNghiep.maNgheNghiep;
            if (_thongTinPhu != null)
                maThongTinPhu = _thongTinPhu.maThongTinPhu;
            else
                maThongTinPhu = "";

            //object

            ngheNghiep = _ngheNghiep;
            if (_thongTinPhu != null)
                thongTinPhu = _thongTinPhu;
            else
                thongTinPhu = null;

        }
        public LyLichSoLuoc(String _maLyLichSoLuoc)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM LyLichSoLuoc WHERE MaLyLichSoLuoc = '" + _maLyLichSoLuoc + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                maLyLichSoLuoc = table.Rows[0]["MaLyLichSoLuoc"].ToString();
                maNgheNghiep = table.Rows[0]["MaNgheNghiep"].ToString();
                maThongTinPhu = table.Rows[0]["MaThongTinPhu"].ToString();
                hoTen = table.Rows[0]["HoTen"].ToString();

                DataRow row = table.Rows[0];

                ngaySinh = row.Field<DateTime>("NgaySinh");
                phai = table.Rows[0]["Phai"].ToString();
                biDanh = table.Rows[0]["BiDanh"].ToString();
                noiSinh = table.Rows[0]["NoiSinh"].ToString();
            }
        }
        public Boolean Save()
        {
            String sqlLyLichSoLuoc = "INSERT INTO LyLichSoLuoc VALUES('" 
                + maLyLichSoLuoc + "',N'" 
                + hoTen + "','" 
                + Database.sqlDateTime(ngaySinh) + "','" 
                + noiSinh + "',N'" 
                + phai + "',N'" 
                + biDanh + "',N'" 
                + ngheNghiep.maNgheNghiep + "','" 
                + maThongTinPhu + "')";
            QueryResult result = myDatabase.Query(sqlLyLichSoLuoc);
            if (result.ReturnCode == 409)
                return false;
            else // save successfully
            {
                ngheNghiep.Save();
                thongTinPhu.Save();
                
            }
            // Return True when everything is OK
            return true;
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("LyLichSoLuoc", "MaLyLichSoLuoc", maLyLichSoLuoc);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE LyLichSoLuoc SET " 
                    + "HoTen = N'" + hoTen 
                    + "', NgaySinh = '" + Database.sqlDateTime(ngaySinh)
                    + "', NoiSinh = N'" + noiSinh 
                    + "',Phai = N'" + phai 
                    + "', BiDanh = N'" + biDanh
                    + "' WHERE MaLyLichSoLuoc = '" + maLyLichSoLuoc + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;

            }
            return true;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM LyLichSoLuoc WHERE MaLyLichSoLuoc = '" + maLyLichSoLuoc + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            else
            {
                ngheNghiep.Delete();
                thongTinPhu.Delete();
                return true;
            }
        }
    }
}
